package com.keep.live.command.schedule;

import cn.hutool.core.util.RuntimeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executors;

import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author ycj yuanchongjie@keep.com
 * @date 2020-12-11 11:24
 */

@Slf4j
@Component
public class TeminateTask {


    private static ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);


    public void kill(String pid, Long time) {
        scheduledExecutorService.schedule(() -> {
            stop(pid);
        }, time, TimeUnit.SECONDS);

    }


    public String stop(String pid) {
        String[] cs = new String[]{"kill", "-9", pid};
        String res = RuntimeUtil.execForStr(cs);
        log.info("end pid {}", res);
        return res;
    }




}
